package com.kongwg.java;

import org.junit.Test;

import java.io.*;
import java.util.stream.Stream;

/**
 * @author kongwg
 * @create 2020-12-27 16:54
 */
public class Java9Test {
    //    @Test
//    public void test() {
//        ClassLoader c1 = this.getClass().getClassLoader();
//        try (InputStream is = c1.getResourceAsStream("hello.txt");
//             FileOutputStream fos = new FileOutputStream("src/hello1.txt")) {
//
//            long l = is.transferTo(fos);
//            System.out.println(l);
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
    @Test
    public void test5() {
        ClassLoader cl = this.getClass().getClassLoader();
        try (InputStream is = cl.getResourceAsStream("hello.txt");
             OutputStream os = new FileOutputStream("src\\hello1.txt")) {
            is.transferTo(os); // 把输入流中的所有数据直接自动地复制到输出流中
            System.out.println("ok");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//    @Test
//    public void test3() {
//        Stream.iterate(0, x -> x < 100, x -> x + 1).forEach(System.out::print);
//    }
//

//    public static void main(String[] args) {
//        ClassLoader cl = Java9Test.class.getClassLoader();
//        try (InputStream is = cl.getResourceAsStream("hello.txt");
//             FileOutputStream os = new FileOutputStream("hello1.txt")) {
//            is.transferTo(os); // 把输入流中的所有数据直接自动地复制到输出流中
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


    @Test
    public void test3(){

        Stream.iterate(0, x -> x + 1).limit(10).forEach(System.out::println);


        //java9中新增的重载的方法
        Stream.iterate(0,x -> x < 100,x -> x + 1).forEach(System.out::println);
    }

    @Test
    public void tets(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("hello.txt");
            fos = new FileOutputStream("hello2.txt");
            fis.transferTo(fos);
            System.out.println("ok");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (fos != null){

                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
